/*===============================================
*   文件名称：homework.c
*   创 建 者：
*   创建日期：2024年09月25日
*   描    述：
================================================*/
#include <stdio.h>
#include <sqlite3.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int usr_reg(sqlite3 *db);
int usr_log(sqlite3 *db,char *o_name);
int usr_del(sqlite3 *db,char *name);
int usr_upd(sqlite3 *db);
int callback(void *para, int f_num, char **f_value, char **f_name);

int main(int argc, char *argv[])
{
    sqlite3 *db;
    char *errmsg;
    if (0 != sqlite3_open("my.db", &db))
    {
        fprintf(stderr, "open db:%s\n", sqlite3_errmsg(db));
        return -1;
    }

    char sql[128] = "create table if not exists userinfo(uname char(20) primary key, uword char(20));";
    if (0 != sqlite3_exec(db, sql, NULL, NULL, &errmsg))
    {
        fprintf(stderr, "create table:%s\n", errmsg);
        return -1;
    }

    int cmd;
    char now_name[20], now_word[20];
    while (1)
    {
        system("clear");
        printf("********************************************\n");
        printf("***1.log***2.reg***3.del***4.upd***5.quit***\n");
        printf("********************************************\n");
        scanf("%d", &cmd);
        getchar();
        printf("%d",cmd);
        switch (cmd)
        {
        case 1:
            if (0 == usr_log(db,now_name))
            {
            }
            break;
        case 2:
            if (0 == usr_reg(db))
            {
            }
            break;
        case 3:
            
            if (0 == usr_del(db,now_name))
            {
                printf("del user:%s",now_name);
            }
            break;
        case 4:
            if (0 == usr_upd(db))
            {
            }
            break;
        case 5:
            return 0;
        }
    }
    return 0;
}

int usr_reg(sqlite3 *db)
{
    char name[20], word[20];
XXX:
    system("clear");
    printf("please input usr name: ");
    scanf("%s", name);
    printf("please input usr password: ");
    scanf("%s", word);
    char sql[128];
    sprintf(sql, "insert into userinfo values('%s', '%s');", name, word);
    if (0 != sqlite3_exec(db, sql, NULL, NULL, NULL))
    {
        printf("账户已存在!\n");
        sleep(1);
        goto XXX;
    }
    return 0;
}

int usr_log(sqlite3 *db,char * o_name)
{
    char name[20], word[20];
XXX:
    system("clear");
    printf("please input usr name: ");
    scanf("%s", name);
    printf("please input usr password: ");
    scanf("%s", word);
    char sql[128];
    sprintf(sql, "select *from userinfo where uname = '%s' and uword = '%s';", name, word);
    int status = 0;
    if (0 != sqlite3_exec(db, sql, callback, &status, NULL))
    {
        return -1;
    }
    if (status == 0)
    {
        puts("登陆失败,账号或密码错误!");
        sleep(1);
        goto XXX;
    }
    puts("登陆成功!");
    strcpy(o_name,name);

    sleep(1);
    return 0;
}

int usr_del(sqlite3 *db,char *name)
{
    char sql[128];
    sprintf(sql, "delete from userinfo where uname = '%s'", name);
    puts(sql);
    
    if (0 != sqlite3_exec(db, sql,NULL, NULL, NULL))
    {
        puts("注销uname失败");
        sleep(1);
        return -1;
    }
    puts("注销成功!");
    sleep(1);
    return 0;
}

int usr_upd(sqlite3 *db)
{
    char name[20], word[20];
XXX:
    system("clear");
    printf("please input usr name: ");
    scanf("%s", name);
    printf("please input usr password: ");
    scanf("%s", word);
    char sql[128];
    sprintf(sql, "update userinfo set word = '%s' where name = '%s';", word, name);
    if (0 != sqlite3_exec(db, sql, NULL, NULL, NULL))
    {
        puts("name不存在，重新输入");
        goto XXX;
    }
    puts("修改成功");
}

int callback(void *para, int f_num, char **f_value, char **f_name)
{
    //*(int *)para = 1;
    int *p = (int *)para;
    *p = 1;
    return 0; // 如果不返回0表示错误
}
